package com.abc.rule.aviator;

import com.googlecode.aviator.AviatorEvaluator;

import java.util.HashMap;
import java.util.Map;

public class Main {

   public static void main(String[] args) throws Exception {

      doGenCommon();

//      doGenSql();


   }

   private static void doGenCommon() {
      AviatorEvaluator.addFunction(new DivideFunction());


      Map<String, Object> paramMap = new HashMap<String, Object>();
      paramMap.put("package", "11");
      paramMap.put("traffic", "");
      paramMap.put("a", 1110);
      paramMap.put("b", 120);
      paramMap.put("c", 20);
//      paramMap.put("gender", "男");
//      paramMap.put("age", 11);
      paramMap.put("if_5g_member", 0);
      paramMap.put("is_unlimit", 0);
      paramMap.put("inner_usageAmount", 40);
      paramMap.put("inner_ratableAmount", 100);
      paramMap.put("balance", 30);
      paramMap.put("reg_time", "2021-01-09");
      paramMap.put("act_createtime", "2020-04-24 00:10:10");
      paramMap.put("nativeNet", 201);
      paramMap.put("activity_id", "[{'10002'}]");

      String configInfo = "div(package,traffic) <= 50";
      String ageInfo = " age > 1  &&  gender == '1'  ";
      String genderInfo = "gender == '男' && if_5g_member == 1";
      String genderInfo2 = "is_unlimit == 0 && div(inner_usageAmount,inner_ratableAmount) <= 50";
      String genderInfo3 = "balance != nil && balance <= 20 ";
      String genderInfo4 = "reg_time >= before(1) ";
      String genderInfo5 = "age != nil";
      String genderInfo6 = "string.contains(activity_id,'10002,100')";
      String genderInfo8 = "string.contains(activity_id,'10005')";
      String genderInfo7 = "inner_usageAmount != nil && inner_ratableAmount != nil && div(inner_usageAmount,inner_ratableAmount) > 0.4    ";
      String genderInfo9 = "act_createtime != nil && monBetween(act_createtime) >=  50  ";
      String genderInfo10 = "act_createtime != nil && dayBetweenNotYear(act_createtime) ==  -1  ";

//      String expression = "a-(b-c)>100";

//      Expression compiledExp = AviatorEvaluator.compile(expression);

//      Expression compiledEx2 = AviatorEvaluator.compile(configInfo);
//      Expression compiledEx2 = AviatorEvaluator.compile(genderInfo);

//      Boolean rst = (Boolean) expression.execute(paramMap);
//      Boolean rst = (Boolean) compiledExp.execute(paramMap);

      if (!AviatorEvaluator.containsFunction("before")) {
         AviatorEvaluator.addFunction(new BeforeFunction());
      }

      if (!AviatorEvaluator.containsFunction("div")) {
         AviatorEvaluator.addFunction(new DivideFunction());
      }

      if (!AviatorEvaluator.containsFunction("monBetween")) {
         AviatorEvaluator.addFunction(new MonthBetweenFunction());
      }
      if (!AviatorEvaluator.containsFunction("dayBetween")) {
         AviatorEvaluator.addFunction(new DayBetweenFunction());
      }
      if (!AviatorEvaluator.containsFunction("dayBetweenNotYear")) {
         AviatorEvaluator.addFunction(new DayBetweenNotYearFunction());
      }

      Boolean result = (Boolean) AviatorEvaluator.execute(genderInfo10, paramMap);

//      Long rst2 = (Long) compiledEx2.execute(paramMap);

//      System.out.println(rst2);
      System.out.println(result);
//      System.out.println("123".substring(0,5));

//      Long num1 = 9789635L;
//      Long num2 = 9707801L;
//      Long num3 = 9784525L;
//      Long num4 = 11444353L;
//      Long num5 = 12166250L;
//      Long num6 = 13582170L;
//      Long num7 = 19810922L;

//      Long total = num1 + num2 + num3 + num4 + num5 + num6 + num7;

//      System.out.println("total=" + total);

//      String str = "Let it go";
//      System.out.println(str.hashCode());

//      String whereSql = "a.sence_id = '10'  and a.5g_offer_nbr = '7360110000100179'  and 1=#{a.dmid.tf_5g_qy_noget_noactive_w}";
//      String offerNbrStr = "let_replace_offer_nbr";
//
//      whereSql= whereSql.replaceAll("5g_offer_nbr", offerNbrStr);

//      System.out.println(whereSql + " contains " + whereSql.contains("5g_offer_nbr"));

   }

   private static void doGenSql() {
      String sql = "select * from app.label_recharge_stats_d30_df t left join app.label_login_and_alias_df a on t.phone_num = a.phone_number where a.alias_recenttime>= DayBefore(7)";

      if (sql.contains("DayBefore")) {
         if (!AviatorEvaluator.containsFunction("DayBefore")) {
            AviatorEvaluator.addFunction(new DayBeforeFunction());

            System.out.println(AviatorEvaluator.execute(sql,
                    null));
         }
      }
   }


//   private static String strToBinstr(String str) {
//      char[] strChar = str.toCharArray();
//      String result = "";
//      for (int i = 0; i < strChar.length; i++) {
//         result += Integer.toBinaryString(strChar[i]) + " ";
//      }
//      return result;
//   }
}
